// 语音播放
const synth = window.speechSynthesis
const message = new SpeechSynthesisUtterance()

function playText(text) {
	message.text = text
	message.lang = 'zh'
	message.rate = 1
	synth.speak(message)
}

$.ajax({
	"url":"/site_admin/",
	"error":errorAjax,
	"beforeSend":start_load_pic,
	"complete":stop_load_pic,
	"dataType":"jsonp",
	"success":function(data){
		if(responseCheck(data)){
			if(data.user_type==="站长"){
				$("#showSite").hide()
				return true;
			}
			var select=document.getElementById("site_select")
			for(var i=0;i<data.content.length;i++){
				var option=document.createElement("option")
				option.textContent=data.content[i].name
				option.value=data.content[i].name
				select.appendChild(option)
			}
		}
	}
})
document.getElementById("q").onclick=function(){
	clearInterval(window.getD)
	Q()
}
document.getElementById("q2").onclick=function(){
	$("#start_date").val("")
	$("#end_date").val("")
	$("#site_select").val("all")
	$("#oil_select").val("all")
	window.getD=setInterval(function(){
			Q(false,"true")
		},1000
		)
}

document.getElementById("reset-aa").onclick=function(){
	$("#start_date").val("")
	$("#end_date").val("")
	$("#site_select").val("all")
	$("#oil_select").val("all")
	$("#tab>tr").remove()
	clearInterval(window.getD)
}
function Q(download,realTime){
	var data={}
	data.start_date=$("#start_date").val()
	data.end_date=$("#end_date").val()
	if(realTime!=="true"){
		if(data.start_date===""){
			showErrorInfo("请选择起始日期！")
			return false;
		}
		if(data.end_date===""){
			showErrorInfo("请选择终止日期！")
			return false;
		}
	}
	console.log(data)

	data.site=$("#site_select").val()
	data.operator=$("#operator").val()
	data.member=$("#member").val()
	data.pay=$("#pay_select").val()
	data.oil=$("#oil_select").val()
	data=JSON.stringify(data)
	$.ajax({
		"url":"/consumption_detail/",
		"error":errorAjax,
		"dataType":"jsonp",
		"data":{"data":data,"real_time":realTime},
		"success":function(data){
			if(responseCheck(data)){
				$("#showErrorInfoDIV").hide("fast");
				document.getElementById("shadow").style.display = "none";
				if (download===true){
					if(data.content.length===0){
						showErrorInfo("当前无数据可下载！")
					}
					else{
						window.location.href=data.url
					}
				}
				$("#title-x").text(data.start_date +" 至 " +data.end_date)
				$("tbody>tr").remove()
				var t2=document.getElementById("t2")
				var tr=document.createElement("tr")

				var td=document.createElement("td")
				td.textContent="合计总金额(优惠前)"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.all_consumption.toFixed(2)+" 元"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="实收总金额(优惠后)"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.real_consumption.toFixed(2)+" 元"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="优惠总金额(优惠)"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.free_consumption.toFixed(2) +" 元"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="合计总销售容量(升)"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.capacity.toFixed(2) +" 升"
				tr.appendChild(td)

				t2.appendChild(tr)



				//第二个表
				var t3=document.getElementById("t3")
				var tr=document.createElement("tr")

				var td=document.createElement("td")
				td.textContent="92# 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.oil["92"].toFixed(2) +"升"
				tr.appendChild(td)

			
				var td=document.createElement("td")
				td.textContent="95# 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.oil["95"].toFixed(2) +"升"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="98# 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.oil["98"].toFixed(2) +"升"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="0# 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.oil["0"].toFixed(2) +"升"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="LNG 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				td.textContent=data.oil["lng"].toFixed(2) +"方"
				tr.appendChild(td)

				var td=document.createElement("td")
				td.textContent="电 销售总量"
				tr.appendChild(td)
				var td=document.createElement("td")
				if(data.oil["电"]===undefined){
					td.textContent="0度"
				}
				else{
					td.textContent=data.oil["电"].toFixed(2)+"度"
				}
				tr.appendChild(td)
				
				t3.appendChild(tr)


				//第二个表




				var tab=document.getElementById("tab")
				for (var i=0;i<data.content.length;i++){
					//语音播报已缴费的信息
					var tr=document.createElement("tr")

					var td=document.createElement("td")
					td.textContent=data.content[i].id
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].date
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].site
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].operator
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].operator_name
					tr.appendChild(td)
		
					var td=document.createElement("td")
					if(data.content[i].member===""){
						td.textContent="--"
					}
					else{
						td.textContent=jiami(data.content[i].member,data.user_type)
					}
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].member_name
					tr.appendChild(td)
		
					var td=document.createElement("td")
					td.textContent=data.content[i].oil
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data.content[i].capacity.toFixed(2)
					tr.appendChild(td)
					
					var td=document.createElement("td")
					var span=document.createElement("span")
					if (data.content[i].pay==="weichat"){
						span.textContent="微信支付"
						span.className="btn btn-success btn-xs"
					}
					else if (data.content[i].pay==="tuanyou"){
						span.textContent="团油支付"
						span.className="btn  btn-xs"
						span.style.backgroundColor="black"
						span.style.color="white"
					}
					else if (data.content[i].pay==="zhifu"){
						span.textContent="支付宝支付"
						span.className="btn btn-primary btn-xs"
					}
					else if (data.content[i].pay==="cash"){
						span.textContent="现金支付"
						span.className="btn btn-danger btn-xs"
					}
					else if (data.content[i].pay==="online"){
						span.textContent="会员支付"
						span.className="btn btn-info btn-xs"
					}
					else if (data.content[i].pay==="shuaka"){
						span.textContent="刷卡支付"
						span.className="btn btn-default btn-xs"
					}
					else if (data.content[i].pay==="jizhang"){
						span.textContent="记账支付"
						span.className="btn btn-warning btn-xs"
					}
					td.appendChild(span)
	
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data.content[i].consumption
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data.content[i].oil_free
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data.content[i].realCash
					tr.appendChild(td)
					
					var td=document.createElement("td")
					var btn=document.createElement("button")
					btn.className="btn btn-danger btn-xs"
					btn.textContent="退账"
					btn.setAttribute("_id",data.content[i].id)
					btn.setAttribute("data",JSON.stringify(data.content[i]))
					btn.onclick=function(){
						clearInterval(window.getD)
						var _id=this.getAttribute("_id")
						var _data=this.getAttribute("data")
						_data=JSON.parse(_data)
						if(_data.pay==="zhifu"){
							method="支付宝支付"
						}
						else if(_data.pay==="weichat"){
							method="微信支付"
						}
						else if(_data.pay==="online"){
							method="会员支付"
						}
						else if(_data.pay==="cash"){
							method="现金支付"
						}
						var t=confirm("请确认退账信息:\n消费金额: ￥"+_data.consumption+"\n实付: ￥"+_data.realCash+"\n支付方式: "+method)
						if(!t){
							window.getD=setInterval(function(){
								Q(false,"true")
								},1000
							)
							return false;
						}
						$.ajax({
							"url":"/consumption_detail/",
							"error":errorAjax,
							"beforeSend":start_load_pic,
							"complete":stop_load_pic,
							"data":{"id":_id},
							"type":"DELETE",
							"success":function(data){
								if(responseCheck(data)){
									alert("操作成功！")
								}
								window.getD=setInterval(function(){
									Q(false,"true")
									},1000
								)
								
							}
						})
					}
					tr.appendChild(td)
					td.appendChild(btn)
					
					tab.appendChild(tr)
		
				}
			}
		}
	})
}
document.getElementById("member").onkeyup=function(){
		if(event.keyCode==13){
			Q()
		}
}
document.getElementById("operator").onkeyup=function(){
		if(event.keyCode==13){
			Q()
		}
}
document.getElementById("download2").onclick=function(){
	Q(true,"true")
}
document.getElementById("download").onclick=function(){
	Q(true,false)
}
window.getD=setInterval(function(){
		Q(false,"true")
	},1000
)
